package com.keli.dt.analyzer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

//import org.apache.log4j.Logger;

import com.keli.dt.model.LaneReport;
import com.keli.dt.model.RoadReport;

public class RoadReportAnalyzer {
	//private Logger logger = Logger.getLogger(this.getClass());
	
	List<LaneReport> laneReports;
	
	public RoadReportAnalyzer(List<LaneReport> laneReports) {
		this.laneReports = laneReports;
	}
	
	/**
	 * 路段过车信息统计分析
	 * @param laneReports 车道过车信息列表
	 * @return
	 */
	public List<RoadReport> analyze() {
		List<RoadReportHelper> helpers = new ArrayList<RoadReportHelper>();
		for (LaneReport lr : laneReports) {
			int index = findHelper(lr.getDETECT_CLASS(),
					lr.getROAD_ID(), helpers);
			if (index == -1) {
				helpers.add(new RoadReportHelper(lr));
			} else {
				RoadReportHelper helper = helpers.get(index);
				helper.addLaneReport(lr);
				helpers.add(helper);
				helpers.remove(index);
			}
		}
		return toRoadReportList(helpers);
	}

	/**
	 * 查询路段过车信息，按设备类型和路段编号分类，类似SQL的GROUP BY子句
	 * @param detectClass 设备类型
	 * @param roadID 路段编号
	 * @param list 路段过车帮助信息列表
	 * @return
	 */
	private int findHelper(String detectClass, BigDecimal roadID, List<RoadReportHelper> helpers) {		
		int index = 0;
		for (RoadReportHelper helper : helpers) {
			if (helper.getDETECT_CLASS().equals(detectClass)
					&& helper.getROAD_ID().equals(roadID)) {
				return index;
			}
			index++;
		}
		return -1;
	}

	/**
	 * 封装路段过车信息
	 * @param helpers 路段过车帮助信息列表
	 * @return
	 */
	private static List<RoadReport> toRoadReportList(
			List<RoadReportHelper> helpers) {
		List<RoadReport> list = new ArrayList<RoadReport>();

		for (RoadReportHelper i : helpers) {
			list.add(i.toRoadReport());
		}

		return list;
	}
}
